package com.sz.app.oss.api.common.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.base.entity.Currency;
import com.sz.biz.common.base.service.CurrencyService;
import com.sz.biz.common.base.dto.CurrencyDto;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.service.I18nBaseService;
import com.sz.common.base.utils.EntityUtils;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Function: CurrencyController <br>
 * Author: xutao <br>
 * Date: 2016-11-30 10:19:00
 */
@RestController
@Api(description = " ", tags = "025、货币管理")
@RequestMapping("/api/v1/biz/currency")
public class CurrencyController extends BaseController {
    @Autowired
    private CurrencyService currencyService;

    @Autowired
    private I18nBaseService i18nService;

    @Value("${i18n.biz.currency}")
    private String i18nTable;

    /**
     * 新增货币
     *
     * @param currencyDto 要保存的数据
     * @return 操作状态
     * @throws Exception 异常信息
     */
    @ApiOperation(value = "新增货币", notes = "新增货币")
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CURRENCY)
    @RequestMapping(value = "", method = {RequestMethod.POST})
    public ResultDto addCurrency(@ApiParam(value = "货币对象", required = true)
                                 @RequestBody CurrencyDto currencyDto) {
        Currency currency = new Currency();

        EntityUtils.copyPropertiesIgnoreNull(currencyDto, currency);

        currencyService.save(currency);
        i18nService.save(currency.getCode(), currencyDto.getI18n(), i18nTable);

        return getSaveSuccessDto(MessagesCodes.MODULE_CURRENCY);
    }
//
//    /**
//     * 查询所有货币
//     *
//     * @param
//     * @return 操作状态
//     * @throws Exception 异常信息
//     */
//    @ApiOperation(value = "查询所有货币", notes = "查询所有货币")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "language", value = "语言信息", dataType = "string", paramType = "path", required = true),
//            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
//            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue = "1"),
//            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query", defaultValue = "10"),
//            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
//            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query"),
//            @ApiImplicitParam(name = "unfilled", value = "未填写项", dataType = "Boolean", required = false, paramType = "query")
//    })
//    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CURRENCY)
//    @RequestMapping(value = "/allcurrencylist/{language}", method = {RequestMethod.GET})
//    public QResultDto getAllCurrency(@PathVariable String language,
//                                     @RequestParam(required = false,defaultValue = "false") boolean unfilled) throws Exception {
//
//        QResultDto qResultDto = this.findI18n(language,null,0,300,"code",true,unfilled);
//
//        return qResultDto;
//    }

    /**
     * 通过货币代码查询货币信息
     *
     * @param q 查询货币关键字 pageIndex 页码 pageSize 页大小
     * @
     */
    @ApiOperation(value = "查询货币", notes = "模糊查询货币")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言信息", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "isAsc", value = "排序类型,true为升序，false为降序", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "sortName", value = "排序的字段名称", required = false, dataType = "string", paramType = "query"),
    })
    @RequestMapping(value = "/currencylist/{language}", method = {RequestMethod.GET})
    public ResultDto getCurrencyByCcode(@PathVariable String language,
                                        @RequestParam(required = false, defaultValue = "") String q,
                                        @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                        @RequestParam(required = false, defaultValue = "10") int pageSize,
                                        @RequestParam(required = false, defaultValue = "code") String sortName,
                                        @RequestParam(required = false, defaultValue = "true") boolean isAsc) {
        q = StringUtils.replaceSqlPattern(q);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        ResultDto resultDto = currencyService.findCurrencyList(q, pageIndex, pageSize, sortName, isAsc, language);
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        return resultDto;

    }

    /**
     * 删除货币
     *
     * @param
     * @return 操作状态
     * @throws Exception 异常信息
     */
    @ApiOperation(value = "删除货币", notes = "删除指定货币")
    @ApiImplicitParam(name = "listIds", value = "货币代码", required = true, dataType = "List", paramType = "body")
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CURRENCY)
    @RequestMapping(value = "deletions", method = {RequestMethod.POST})
    public ResultDto delCurrency(@RequestBody List<Integer> listIds) {

        currencyService.deleteByCode(listIds);

        return getDeleteSuccessDto(MessagesCodes.MODULE_CURRENCY);
    }

    /**
     * 修改货币
     *
     * @param
     * @return 操作状态
     * @throws Exception 异常信息
     */
    @ApiOperation(value = "修改货币", notes = "修改货币")
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CURRENCY)
    @RequestMapping(value = "", method = {RequestMethod.PUT})
    public ResultDto updateCurrency(@ApiParam(value = "货币对象", required = true)
                                    @RequestBody CurrencyDto currencyDto) {

        Currency currency = new Currency();

        EntityUtils.copyPropertiesIgnoreNull(currencyDto, currency);

        currencyService.updateCurrencyById(currency);

        i18nService.save(currency.getCode(), currencyDto.getI18n(), i18nTable);

        return getUpdateSuccessDto(MessagesCodes.MODULE_CURRENCY);
    }

    /**
     * 根据条件获取国际化数据集合
     *
     * @param language  语言信息
     * @param q         参数信息
     * @param pageIndex 哪页
     * @param pageSize  分页大小
     * @param sort      排序字段的名称
     * @param asc       true asc false desc
     * @return 查询的国际化数据集合
     * @throws Exception 异常信息
     */
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "查询i18n", notes = "根据指定条件获取国际化数据集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language", value = "语言信息", dataType = "string", paramType = "path", required = true),
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "unfilled", value = "未填写项", dataType = "Boolean", required = false, paramType = "query")
    })
    @RequestMapping(value = "i18n/list/{language}", method = RequestMethod.GET)
    public QResultDto findI18n(@PathVariable String language, @RequestParam(required = false) String q,
                               @RequestParam(required = false, defaultValue = "1") int pageIndex,
                               @RequestParam(required = false, defaultValue = "10") int pageSize,
                               @RequestParam(required = false, defaultValue = "id") String sort,
                               @RequestParam(required = false, defaultValue = "true") Boolean asc,
                               @RequestParam(required = false, defaultValue = "false") boolean unfilled) {
        q = StringUtils.replaceSqlPattern(q);
        QResultDto qResultDto = currencyService.findI18nByParam(new QueryParams(pageSize, pageIndex, q, sort, asc, language), unfilled);

        return qResultDto;
    }

    /**
     * 凭借货币代码查找对应货币国际化信息
     *
     * @param code:货币代码
     * @return 返回对应货币列表
     */
    @ApiOperation(value = "根据货币ID查询货币信息", notes = "根据货币ID查询货币信息")
    @ApiImplicitParam(name = "code", value = "货币代码", dataType = "string", paramType = "path", required = true)
    @RequestMapping(value = "/{code}", method = RequestMethod.GET)
    public QResultDto findCurrencyByCode(@PathVariable String code) {
        code = StringUtils.replaceSqlPattern(code);
        QResultDto qResultDto = currencyService.findCurencyByCode(code);

        return qResultDto;
    }


    /**
     * 新增货币国际化.
     *
     * @param i18nDtos 要保存的数据
     * @return 操作状态
     * @throws Exception 异常信息
     */
    @ApiOperation(value = "新增货币国际化", notes = "给指定货币增加国际化")
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CURRENCY)
    @RequestMapping(value = "/i18n", method = {RequestMethod.POST})
    public ResultDto saveCurrencyI18n(@ApiParam(value = "货币国际化对象", required = true)
                                      @RequestBody List<I18nDto> i18nDtos) {

        i18nService.save(i18nDtos, i18nTable);
        currencyService.refreshCache();
        ResultDto result = this.getSaveI18nSuccessDto();
        return result;
    }

    /**
     * 获取所有货币的代码及名称
     *
     * @param
     */
    @ApiOperation(value = "获取所有货币的代码及名称", notes = "获取所有货币的代码及名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字",
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页 默认第一页",
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小",
                    dataType = "int", paramType = "query")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultDto getCurrencyByCcode(@RequestParam(defaultValue = "") String q,
                                        @RequestParam(defaultValue = "1") int pageIndex,
                                        @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {

        QResultDto qResultDto = currencyService.findCurrencies(q, pageIndex, pageSize);

        return qResultDto;

    }

    /**
     * 凭借货币代码以及language查找对应货币
     *
     * @param code:货币代码
     * @return 返回对应货币列表
     */
    @ApiOperation(value = "凭借货币代码以及language查找对应货币", notes = "凭借货币代码以及language查找对应货币")
    @ApiImplicitParam(name = "code", value = "货币代码", dataType = "string", paramType = "path", required = true)
    @RequestMapping(value = "/language/{code}", method = RequestMethod.GET)
    public QResultDto findSingleCurencyByCode(@PathVariable String code) {

        code = StringUtils.replaceSqlPattern(code);

        CurrencyDto currencyDto = currencyService.findSingleCurencyByCode(code);

        QResultDto qResultDto = new QResultDto();

        qResultDto.setData(currencyDto);

        return qResultDto;
    }

}
